define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'zbtask/drop/index',
                    add_url: 'zbtask/drop/add',
                    edit_url: 'zbtask/drop/edit',
                    del_url: 'zbtask/drop/del',
                    multi_url: 'zbtask/drop/multi',
                    table: 'hz_zbtask_drop',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                searchFormVisible: true,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.username', title: __('User.username'),operate: 'LIKE'},
                        {field: 'orders.task_sn', title: '任务订单编号', operate: 'LIKE'},
                        {field: 'remark', title: '取消原因', operate: false},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'admin.username', title: '处理人'},
                        {field: 'verify_time', title: __('Verify_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'check',
                                    text: '审核订单',
                                    title: '审核订单',
                                    classname: 'btn btn-xs btn-warning btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: 'zb.drop/check',
                                    openOptions: {
                                        title: '是否派单，拒绝该订单会无效',
                                        btn: ['确认派单', '拒绝派单'],
                                        id: 'check-order'
                                    },
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        $("#table").bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if(row.status != 0 ) {
                                    $(table).data("operate-check", null);
                                }
                                $(table).data("operate-edit", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});